<template>
  <div class="app-container" style="padding: 5px;">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dms:dms_case_model_catalog:add']"
        >新增</el-button>
      </el-col>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery" />
        </el-tooltip>
        <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch" />
        </el-tooltip>
      </div>
    </el-row>
    <el-row :gutter="10">
      <el-col :span="8">
        <el-card>
        <el-table
          v-loading="loading"
          :data="dms_case_model_catalogList"
          row-key="id"
          highlight-current-row
          @current-change="showCaseModelHandle"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        >
          <!-- <el-table-column label="父Id" align="center" prop="parentId" />
          <el-table-column label="类型 " align="center" prop="type" :formatter="typeFormat" />
          <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
          <el-table-column label="范围" align="center" prop="scope" :formatter="scopeFormat" />
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column> -->
          <el-table-column label="名称" prop="name"/>
          <el-table-column label="操作" class-name="small-padding">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['dms:dms_case_model_catalog:edit']"
              ></el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['dms:dms_case_model_catalog:remove']"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        </el-card>
      </el-col>
      <el-col :span="16">
        <el-card>
          <CaseModel ref="caseModelRef" :caseModelId="caseModelId" :showCaseModel="showCaseModel"></CaseModel>
        </el-card>
      </el-col>
    </el-row>


    <!-- 添加或修改病例模版目录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="父Id" prop="parentId">
          <treeselect v-model="form.parentId" :options="dms_case_model_catalogOptions" :normalizer="normalizer" placeholder="请选择父Id" />
        </el-form-item>
        <el-form-item label="类型 ">
          <el-select v-model="form.type" placeholder="请选择类型 ">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="范围" prop="scope">
           <el-select v-model="form.scope" placeholder="请选择范围">
            <el-option
              v-for="dict in scopeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDms_case_model_catalog, getDms_case_model_catalog, delDms_case_model_catalog, addDms_case_model_catalog, updateDms_case_model_catalog, exportDms_case_model_catalog } from "@/api/dms/dms_case_model_catalog";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
// 导入病例模板组件
import CaseModel from '../dms_case_model/index'

export default {
  name: "Dms_case_model_catalog",
  components: { Treeselect, CaseModel },
  data() {
    return {
      currentRow: null,
      // 单击模板目录时传递给子组件的病例模板id
      caseModelId: '',
      showCaseModel: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 病例模版目录表格数据
      dms_case_model_catalogList: [],
      // 病例模版目录树选项
      dms_case_model_catalogOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型 字典
      typeOptions: [],
      // 状态字典
      statusOptions: [],
      // 范围字典
      scopeOptions: [],
      // 查询参数
      queryParams: {
        parentId: null,
        type: null,
        status: null,
        scope: null,
        name: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        scope: [
          { required: true, message: "范围不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("dms_case_model_catalog_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("model_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("model_scope").then(response => {
      this.scopeOptions = response.data;
    });

  },
  methods: {

    /** 显示对应病历模板数据 */
    showCaseModelHandle(row, column, event) {
      if(row.type == 2) { // 1目录2模板
        this.showCaseModel = true
        this.caseModelId = row.modelId
        // 调用子组件的方法
        this.$refs.caseModelRef.handleLoad(row.modelId)
      } else {
        this.showCaseModel = false
      }
    },
    /** 查询病例模版目录列表 */
    getList() {
      this.loading = true;
      listDms_case_model_catalog(this.queryParams).then(response => {
        this.dms_case_model_catalogList = this.handleTree(response.data, "id", "parentId");
        this.loading = false;
      });
    },
    /** 转换病例模版目录数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
	/** 查询部门下拉树结构 */
    getTreeselect() {
      listDms_case_model_catalog().then(response => {
        this.dms_case_model_catalogOptions = [];
        const data = { id: 0, name: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "parentId");
        this.dms_case_model_catalogOptions.push(data);
      });
    },
    // 类型 字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 范围字典翻译
    scopeFormat(row, column) {
      return this.selectDictLabel(this.scopeOptions, row.scope);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        level: null,
        type: null,
        status: 0,
        modelId: null,
        scope: null,
        ownId: null,
        createTime: null,
        name: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
	    this.getTreeselect();
      this.open = true;
      this.title = "添加病例模版目录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
	  this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.id;
      }
      getDms_case_model_catalog(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改病例模版目录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDms_case_model_catalog(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addDms_case_model_catalog(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除病例模版目录编号为"' + row.id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDms_case_model_catalog(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    }
  }
};
</script>

<style scoped>
.app-container /deep/ .el-table td{
  padding: 5px 0;
}

.app-container /deep/ .el-form-item {
  line-height: 32px;
  padding-bottom: 10px;
  margin-bottom: 0 !important;
}
</style>
